//
// Created by Administrator on 2021/11/30.
//

#include "Interface.h"

#include "iostream"
using namespace std;

void displayMenu(DonorList &donorList) {
    string yesOrNo;
    do {
        cout
            << "***********************************************************"
            << endl
            << "                         MAIN MENU                         "
            << endl
            << "***********************************************************"
            << endl << endl;
        cout << "Select one of the following:" << endl << endl
             << "    1. Add a donor" << endl
             << "    2. Delete a donor" << endl
             << "    3. Print all donors" << endl
             << "    4: To exit" << endl << endl
             << "  => Enter your selection:";
        processSelection(donorList);
        cout << endl << "  => Would you like to continue? (y/n)";
        cin >> yesOrNo;
        cout << endl;
    } while (yesOrNo == "y");
}

void processSelection(DonorList &donorList) {
    int selection = -1;
    cin >> selection;
    cout << endl;
    if (selection == 1) {
        addDonor(donorList);
    } else if (selection == 2) {
        deleteDonor(donorList);
    } else if (selection == 3) {
        printAllDonors(donorList);
    } else if (selection == 4) {
        cout << "  => Thank you for visiting our site!" << endl;
        return;
    }else {
        cout << "  => Sorry. That is not a selection." << endl;
    }
}

void addDonor(DonorList &donorList) {
    cout << "  => Enter donor's first name:";
    string firstName;
    cin >> firstName;
    cout << endl << "  => Enter donor's last name:";
    string lastName;
    cin >> lastName;
    cout << endl << "  => Enter donor's membership number:";
    int number;
    cin >> number;
    cout << endl << "  => Enter donor's donated: $";
    double donated;
    cin >> donated;

    donorList.addDonor(firstName, lastName, number, donated);
    cout << endl << "  => Donor has been added." << endl;
}

void deleteDonor(DonorList &donorList) {
    if (donorList.isEmpty()) {
        cout << "The database has no donors." << endl;
        return;
    }
    cout << "  => Enter donor's membership number:";
    int number;
    cin >> number;
    bool searchResult = donorList.searchID(number);
    if (searchResult) {
        donorList.deleteDonor(number);
    }
    cout << "  => Donor has been deleted." << endl;
}

void printAllDonors(DonorList &donorList) {
    donorList.printAllDonors();
}
